const {
  createOrUpdate,
  findCarts,
  updateCart,
  removeCarts,
  batchSelectedCarts,
  queryTotalCarts,
} = require("../service/cart.service");
const {
  addCartFail,
  addCartError,
  findCartsFail,
  cartFormatError,
  updateCartFail,
  updateCartError,
  deleteCartsFail,
  deleteCartsError,
  batchSelectedCartsFail,
  batchSelectedCartsError,
  queryTotalCartsFail,
  queryTotalCartsError,
} = require("../constant/error.type");

class CartController {
  /**
   * 将商品添加到购物车
   * @param {*} ctx 请求上下文
   */
  async add(ctx) {
    try {
      const user_id = ctx.state.user.id;
      const goods_id = ctx.request.body.goods_id;
      const res = await createOrUpdate(user_id, goods_id);
      if (res) {
        ctx.body = {
          code: 0,
          message: "添加到购物车成功",
          result: res,
        };
      } else {
        return ctx.app.emit("error", addCartFail, ctx);
      }
    } catch (error) {
      console.error(error);
      return ctx.app.emit("error", addCartError, ctx);
    }
  }
  /**
   * 查询购物车列表
   * @param {*} ctx 请求上下文
   */
  async findAll(ctx) {
    try {
      const { pageNum = 1, pageSize = 10 } = ctx.request.query;
      const res = await findCarts(pageNum, pageSize);
      if (res) {
        ctx.body = {
          code: 0,
          message: "获取购物车列表成功",
          result: res,
        };
      } else {
        return ctx.app.emit("error", findCartsFail, ctx);
      }
    } catch (error) {
      console.error(error);
      return ctx.app.emit("error", findCartsError, ctx);
    }
  }
  /**
   * 更新购物车项
   * @param {*} ctx 请求上下文
   * @returns
   */
  async update(ctx) {
    try {
      const { id } = ctx.request.params;
      const { number, selected } = ctx.request.body;
      // 如果两个参数都不传，返回格式错误
      if (number === undefined && selected === undefined) {
        cartFormatError.message = "number和selected不能同时为空";
        return ctx.app.emit("error", cartFormatError, ctx);
      }
      const res = await updateCart({ id, number, selected });
      if (res) {
        ctx.body = {
          code: 0,
          message: "更新购物车项成功",
          result: res,
        };
      } else {
        return ctx.app.emit("error", updateCartFail, ctx);
      }
    } catch (error) {
      console.error("更新购物车项出错：", error);
      return ctx.app.emit("error", updateCartError, ctx);
    }
  }
  /**
   * 删除购物车项
   * @param {*} ctx 请求上下文
   */
  async remove(ctx) {
    try {
      const { ids } = ctx.request.body;
      const res = await removeCarts(ids);
      if (res) {
        ctx.body = {
          code: 0,
          message: "删除购物车项成功",
          result: res,
        };
      } else {
        return ctx.app.emit("error", deleteCartsFail, ctx);
      }
    } catch (error) {
      console.error("删除购物车项出错：", error);
      return ctx.app.emit("error", deleteCartsError, ctx);
    }
  }
  async batchSelected(ctx) {
    try {
      const user_id = ctx.state.user.id;
      const { selected } = ctx.request.body;
      const res = await batchSelectedCarts(user_id, selected);
      if (res) {
        ctx.body = {
          code: 0,
          message: selected ? "购物车全选成功" : "购物车全不选成功",
          result: res,
        };
      } else {
        return ctx.app.emit("error", batchSelectedCartsFail, ctx);
      }
    } catch (error) {
      console.error("批量更新购物车的选中状态出错：", error);
      return ctx.app.emit("error", batchSelectedCartsError, ctx);
    }
  }
  async total(ctx) {
    try {
      const user_id = ctx.state.user.id;
      const res = await queryTotalCarts(user_id);
      if (res) {
        ctx.body = {
          code: 0,
          message: "获取购物车总数成功",
          result: res,
        };
      } else {
        return ctx.app.emit("error", queryTotalCartsFail, ctx);
      }
    } catch (error) {
      console.error("查询购物车总数失败", error);
      return ctx.app.emit("error", queryTotalCartsError, ctx);
    }
  }
}

module.exports = new CartController();
